package cloud.take.off.module.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@ToString
@NoArgsConstructor
@ApiModel(value = "返回包装类", description = "返回包装类")
public class R<T> extends ResponseResult {
    public R(ResultCode resultCode) {
        super(resultCode);
    }

    public R(String errorInfo) {
        this.success = false;
        this.code = 500;
        this.message = errorInfo;
    }

    @ApiModelProperty(value = "返回参数")
    private T pageData;

    @ApiModelProperty(value = "是否有权限")
    boolean isAuthority=true;

    public static <T> R<T> data(T data) {
        R r = new R(CommonCode.SUCCESS);
        r.setPageData(data);
        return r;
    }

    public static <T> R<T> data(T data, CommonCode commonCode) {
        R r = new R(commonCode);
        r.setPageData(data);
        return r;
    }

    public static <T> R<T> status(boolean flag) {
        return flag ? success() : fail();
    }

    public static <T> R<T> success() {
        return new R(CommonCode.SUCCESS);
    }

    public static <T> R<T> fail() {
        return new R(CommonCode.FAIL);
    }

    public static <T> R<T> fail(ResultCode code, String message) {
        return new R(CustomCode.builder().message(message).code(code.code()).success(false).build());
    }
    public static <T> R<T> fail(String message) {
        return new R(CustomCode.builder().message(message).code(1999).success(false).build());
    }

    public static <T> R<T> error() {
        return new R(CommonCode.ERROR);
    }

}
